<?php
namespace App\Extend\Ali;

use App\Server\CurlApi;

require BASE_PATH.'/app/Extend/Ali/Aop/AopClient.php';
require BASE_PATH.'/app/Extend/Ali/Aop/AopClientDemo.php';
require BASE_PATH.'/app/Extend/Ali/Aop/AlipayUserInfoAuthRequest.php';
require BASE_PATH.'/app/Extend/Ali/Aop/AlipaySystemOauthTokenRequest.php';

class AliSdk{

    /*
     * 支付宝小程序通过code获取token  原始方法
     */
    public function getTokenDemo($code){
        $aop = new \AopClientDemo();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = config('web.ali_account.appid');
        $aop->rsaPrivateKey = config('web.ali_account.rsaPrivateKey');
        $aop->alipayrsaPublicKey=config('web.ali_account.alipayrsaPublicKey');
//        $aop->alipayrsaPublicKey=config('web.ali_account.rsaPublicKey');
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $request = new \AlipaySystemOauthTokenRequest ();
        $request->setGrantType("authorization_code");
        $request->setCode($code);
        $request->setRefreshToken("authusrB75a876ed2e91419b86f675bf67eccX39");
        $data = $aop->execute($request);
        return $data;
    }

    /*
     * 支付宝小程序通过code获取token  修改版方法
     */
    public function getToken($code){
        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = config('web.ali_account.appid');
        $aop->rsaPrivateKey = config('web.ali_account.rsaPrivateKey');
        $aop->alipayrsaPublicKey=config('web.ali_account.alipayrsaPublicKey');
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $request = new \AlipaySystemOauthTokenRequest ();
        $request->setGrantType("authorization_code");
        $request->setCode($code);
        $request->setRefreshToken("authusrB75a876ed2e91419b86f675bf67eccX39");
        $data = $aop->execute($request);
        return $data;
    }

    /*
     * 支付宝用户登录授权  修改版方法
     */
    public function userReg(){
        $aop = new \AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = config('web.ali_account.appid');
        $aop->rsaPrivateKey = config('web.ali_account.rsaPrivateKey');
        $aop->alipayrsaPublicKey = config('web.ali_account.alipayrsaPublicKey');
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'utf-8';
        $aop->format = 'json';
        $request = new \AlipayUserInfoAuthRequest();
        $request->setBizContent("{" .
            "      \"scopes\":[" .
            "        \"auth_base\"" .
            "      ]," .
            "\"state\":\"init\"" .
            "  }");
        $post_data = $aop->pageExecute($request);
        return $post_data;
    }
}